import {
  verifyEmail
} from "../utils/util"
Page({

  data: {
    type: 1,
    name: '',
    taxpayer_number: '',
    remarks: '',
    email: '',

    money: 0,
    list: [],
    orderids: '',
  },


  onLoad(options) {
    let money = wx.getStorageSync('invoice_check').money;
    let arr = wx.getStorageSync('invoice_check').list;
    let list = [];
    let orderids = [];
    arr.forEach((v, i) => {
      orderids.push(v.id)
      list.push({
        name: v.title_1,
        specifications: '',
        company: '',
        number: 1,
        unit_price: v.money,
        sum_money: v.money
      })
    })
    this.setData({
      money,
      orderids: orderids.join(','),
      list
    })
  },

  change_type(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      type
    })
  },

  change_inp(e) {
    let val = e.currentTarget.dataset.val;
    this.setData({
      [val]: e.detail.value
    })
  },

  bottomTap() {
    let obj = {}
    if (!this.data.name) {
      wx.showToast({
        title: this.data.type == 1 ? '请填写单位名称' : '请填写需要开具发票的姓名',
        icon: 'none'
      })
      return
    }
    if (!this.data.taxpayer_number) {
      wx.showToast({
        title: this.data.type == 1 ? '请填写纳税人识别号' : '请填写身份证号',
        icon: 'none'
      })
      return
    }
    if (!this.data.email) {
      wx.showToast({
        title: '请填写您的邮箱地址',
        icon: 'none'
      })
      return
    }
    if (!verifyEmail(this.data.email)) {
      wx.showToast({
        title: '请填写正确的邮箱地址',
        icon: 'none'
      })
      return
    }
    getApp().showModal('开取发票将收取3%税点', true).then(() => {
      getApp().globalData.posts('/api/app/invoice/sorting/ouyoung_api/add', {
        ...obj,
        name: this.data.name,
        remarks: this.data.remarks,
        taxpayer_number: this.data.taxpayer_number,
        orderids: this.data.orderids,
        email: this.data.email,
      }).then(res => {
        let data = res.data.data;
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success:(res)=>{
            getApp().showModal('开取发票成功').then(() => {
              wx.reLaunch({
                url: '/pages/index/blank',
              })
            })
          }
        })
      })
    })
  },

})